package com.cy.mall.service.order.impl;

import com.cy.mall.comm.constant.Constants;
import com.cy.mall.dao.mapper.order.OrderFinalStatusMapper;
import com.cy.mall.model.domain.order.OrderFinalStatus;
import com.cy.mall.model.enums.ThirdSource;
import com.cy.mall.model.enums.order.GoodsOrderStatus;
import com.cy.mall.model.enums.order.OrderFinalStatusE;
import com.cy.mall.service.BaseService;
import com.cy.mall.service.order.OrderStatusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description 订单状态逻辑处理 service
 * @Author tanyilin
 * @Date 2019/3/29 18:06
 */
@Service
public class OrderStatusServiceImpl extends BaseService implements OrderStatusService {

    @Autowired
    private OrderFinalStatusMapper orderFinalStatusMapper;

    @Override
    public OrderFinalStatusE getOrderFinalStatus(ThirdSource thirdSource, Long productId, String thirdStatus, GoodsOrderStatus goodsOrderStatus) {

        OrderFinalStatusE finalStatus = null;
        switch (thirdSource) {
            case YUNKEDAI:  //云科贷
                switch (thirdStatus) {
                    case "1":
                        if(goodsOrderStatus == GoodsOrderStatus.NO_BUY) {
                            finalStatus = OrderFinalStatusE.APPLYING;
                        }
                        break;
                    case "2":
                        if(goodsOrderStatus == GoodsOrderStatus.NO_BUY) {
                            finalStatus = OrderFinalStatusE.AUDITING;
                        }
                        break;
                    case "3":
                        if(goodsOrderStatus == GoodsOrderStatus.NO_BUY) {
                            finalStatus = OrderFinalStatusE.DECISION_PASS;
                        }
                        if(goodsOrderStatus == GoodsOrderStatus.AUDITING) {
                            finalStatus = OrderFinalStatusE.GOODS_ORDER_AUDITING;
                        }
                        if(goodsOrderStatus == GoodsOrderStatus.AUDIT_PASS) {
                            finalStatus = OrderFinalStatusE.WITHDRAW_WAIT;
                        }
                        if(goodsOrderStatus == GoodsOrderStatus.AUDIT_REJECT) {
                            finalStatus = OrderFinalStatusE.GOODS_ORDER_AUDIT_REJECT;
                        }
                        break;
                    case "4":
                        if(goodsOrderStatus == GoodsOrderStatus.NO_BUY) {
                            finalStatus = OrderFinalStatusE.DECISION_REJECT;
                        }
                        break;
                    case "5":
                        if(goodsOrderStatus == GoodsOrderStatus.AUDIT_PASS) {
                            finalStatus = OrderFinalStatusE.LOANING;
                        }
                        break;
                    case "6":
                        if(goodsOrderStatus == GoodsOrderStatus.AUDIT_PASS) {
                            finalStatus = OrderFinalStatusE.LOAN_SUCCESS;
                        }
                        break;
                    case "7":
                        if(goodsOrderStatus == GoodsOrderStatus.AUDIT_PASS) {
                            finalStatus = OrderFinalStatusE.LOAN_FAIL;
                        }
                        break;
                    case "8":
                        if(goodsOrderStatus == GoodsOrderStatus.AUDIT_PASS) {
                            finalStatus = OrderFinalStatusE.REPAY_COMPLETED;
                        }
                        break;
                    default:
                        break;
                }
                break;
            default:
                break;
        }

//        //根据产品查询，该产品定制的订单状态值
//        OrderFinalStatus dbOrderFinalStatus = new OrderFinalStatus();
//        dbOrderFinalStatus.setProductId(productId);
//        dbOrderFinalStatus.setStatus(finalStatus);
//        dbOrderFinalStatus = orderFinalStatusMapper.selectOne(dbOrderFinalStatus);
//        finalStatusText = dbOrderFinalStatus.getStatusText();

//        Map<String, Object> statusMap = new HashMap<>();
//        statusMap.put("finalStatus", finalStatus);
//        statusMap.put("finalStatusText", finalStatus.getText());

        return finalStatus;
    }

    @Override
    public OrderFinalStatusE getOrderFinalStatus(Long productId, String thirdStatus, String rejectCode) {

        OrderFinalStatusE finalStatus = null;

        if(Constants.JYD_PRODUCTID.equals(productId)) {
            switch (thirdStatus) {
                case "A001": //准入审核中
                    finalStatus = OrderFinalStatusE.ADMISSION_AUDITING;
                    break;
                case "A002": //审核拒绝：准入拒绝（系统拒绝）、资料审核拒绝（人工拒绝）
                    if("B001".equals(rejectCode)) {
                        finalStatus = OrderFinalStatusE.ADMISSION_REJECT;
                    }
                    if("B002".equals(rejectCode)) {
                        finalStatus = OrderFinalStatusE.MATERIAL_AUDIT_REJECT;
                    }
                    break;
                case "A003": //准入通过
                    finalStatus = OrderFinalStatusE.ADMISSION_PASS;
                    break;
                case "A004": //人工审核通过-待签约
                    finalStatus = OrderFinalStatusE.SIGN_WAIT;
                    break;
                case "A005": //签约完成-放款中
                    finalStatus = OrderFinalStatusE.LOANING;
                    break;
                case "A006": //放款失败
                    finalStatus = OrderFinalStatusE.LOAN_FAIL;
                    break;
                case "A007": //放款成功
                    finalStatus = OrderFinalStatusE.LOAN_SUCCESS;
                    break;
                case "A008": //客户取消
                    finalStatus = OrderFinalStatusE.CUSTOMER_CANCEL;
                    break;
                case "A009": //还款中
                    finalStatus = OrderFinalStatusE.REPAYING;
                    break;
                case "A010": //已结清
                    finalStatus = OrderFinalStatusE.REPAY_COMPLETED;
                    break;
                case "A011": //提交资料审核中
                    finalStatus = OrderFinalStatusE.MATERIAL_AUDITING;
                    break;
                default:
                    break;
            }
        }

        return finalStatus;
    }
}
